using System;
using System.Web.Security;
using System.Web.UI.WebControls;
using SpiderMaster.AspNet.TradingSystem.BusinessLogicLayer;
using SpiderMaster.AspNet.TradingSystem.DataAccessLayer;

namespace SpiderMaster.AspNet.TradingSystem.UI
{
    public partial class ManagePhotos_aspx : TradingCenterPage
    {
        public int AdId
        {
            get
            {
                if (ViewState["AdId"] != null)
                    return (int)ViewState["AdId"];
                else
                    return 0;
            }
            set { ViewState["AdId"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SiteSettings s = SiteSettings.Current;

                int adId = 0;
                string adIdQs = Request.QueryString["id"];

                AdsDataComponent.AdsRow adRow = null;
                bool isAdministration = Roles.IsUserInRole(DefaultValues.AdministratorRole);

                if (adIdQs != null && Int32.TryParse(adIdQs, out adId))
                {
                    this.AdId = adId;
                    adRow = AdsDB.GetAdById(adId);
                }

                // only allow the actual member and Administrators to edit photos
                if (adRow != null && (adRow.MemberId == TradingCenterProfileCommon.Current.MemberId || isAdministration))
                {
                    AdTitleLabel.Text = adRow.Title;
                    string idQs = "id=" + Server.UrlEncode(adIdQs);

                    ShowAdLink.NavigateUrl = "~/ShowAd.aspx?" + idQs;
                    BackToEditAdLink.NavigateUrl = "~/EditAd.aspx?" + idQs;
                    BackToEditAdLink.Visible = (isAdministration || s.AllowUsersToEditAds);
                    if (isAdministration && !s.AllowUsersToEditAds)
                        BackToEditAdLink.Text += " (Administration)";
                }
                else
                {
                    // if the above test fails, re-direct to MyAds
                    Response.Redirect("~/MyAds.aspx", true);
                }

                MainUploadsPanel.Visible = s.AllowImageUploads;
                NoUploadsPanel.Visible = !s.AllowImageUploads;
                if (s.MaxPhotosPerAd == 1)
                    NumTotalUploadLabel.Text = "1 photo";
                else
                    NumTotalUploadLabel.Text = s.MaxPhotosPerAd.ToString() + " photos";

            }
        }

        protected void UploadPhotoDetailsView_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            byte[] uploadedOriginal = e.Values["UploadBytes"] as byte[];
            if (uploadedOriginal == null || uploadedOriginal.Length == 0)
            {
                e.Cancel = true;
                UploadErrorMessage.Visible = false;
            }
            else
            {
                try
                {
                    // adding resized photo bytes to arguments

                    byte[] fullSize = PhotosDB.ResizeImageFile(uploadedOriginal, PhotoSize.Full);
                    byte[] mediumSize = PhotosDB.ResizeImageFile(uploadedOriginal, PhotoSize.Medium);
                    byte[] smallSize = PhotosDB.ResizeImageFile(uploadedOriginal, PhotoSize.Small);

                    e.Values.Add("bytesFull", fullSize);
                    e.Values.Add("bytesMedium", mediumSize);
                    e.Values.Add("bytesSmall", smallSize);

                    // removing original upload bytes from arguments
                    e.Values.Remove("UploadBytes");

                    // if this is the first ad photo uploaded, use it as the preview
                    bool useAsPreview = (PhotoGridView.Rows.Count == 0);
                    e.Values.Add("useAsPreview", useAsPreview);
                    UploadErrorMessage.Visible = false;
                }
                catch
                {
                    // image format was not acceptable
                    e.Cancel = true;
                    UploadErrorMessage.Visible = true;
                }
            }
        }
        protected void PhotoDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
        {
            AdsDataComponent.PhotosDataTable photos = e.ReturnValue as AdsDataComponent.PhotosDataTable;
            SiteSettings settings = SiteSettings.Current;
            if (photos != null)
            {
                PreviewTipPanel.Visible = photos.Rows.Count > 1;
                this.UploadPhotoDetailsView.Visible = (photos.Rows.Count < settings.MaxPhotosPerAd);
            }
            else
                this.UploadPhotoDetailsView.Visible = (settings.MaxPhotosPerAd > 0);
        }
        protected void PhotoGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("SelectAsPreview"))
            {
                int photoId = Convert.ToInt32(e.CommandArgument);
                PhotosDB.SetAdPreviewPhoto(this.AdId, photoId);
                PhotoGridView.DataBind();
            }
        }
    }
}
